/*var login = function() {

    var handleLogin = function() {

        $('.login-form').validate({
            errorElement: 'span',
            errorClass: 'help-block',
            focusInvalid: false,
            rules: {
                username: {
                    required: true
                },
                password: {
                    required: true
                }
            },

            messages: {
                username: {
                    required: "用户名不能为空"
                },
                password: {
                    required: "密码不能为空"
                }
            },

            invalidHandler: function(event, validator) {
                $('.alert-danger', $('.login-form')).show();
            },

            highlight: function(element) {
                $(element)
                    .closest('.form-group').addClass('has-error');
            },

            success: function(label) {
                label.closest('.form-group').removeClass('has-error');
                label.remove();
            },

            errorPlacement: function(error, element) {
                error.insertAfter(element.closest('.input-icon'));
            },

            submitHandler: function(form) {
                form.submit();
            }
        });

       
    }

    return {
        init: function() {
            handleLogin();
        }

    };

}();*/

$(function() {
	 $('.login-form input').keypress(function(e) {
         if (e.which == 13) {
        	 $(".logins").trigger('click');
              $('.login-form').submit();
         }
     });

		$(".logins").click(function(){
			

	
		var loginName=$("input[name='loginName']").val();
		var  password=$("input[name='password']").val();
	
		if (""==loginName.trim()||0==loginName.trim().length) {
			$('.alert-danger span').text("格式非法: 请重新输入用户名");
			 $('.alert-danger', $('.login-form')).show();
			 return false;
		}
		if (""==password.trim()||0==password.trim().length) {
			$('.alert-danger span').text("格式非法: 请重新输入密码");
			$('.alert-danger', $('.login-form')).show();
			 return false;
		}
		$.ajax({
			url : '/logins',
			type : 'post',
			dataType : 'json',
			contentType:"application/json; charset=utf-8",
			async: false,
			data : JSON.stringify({loginName:loginName,password:password}),
		}).done(function(r) {
			if (r.status == '1') { 
				$(".login-form").submit();
				
			} else if (r.status == '2') {
				$('.alert-danger span').text(r.message);
				$('.alert-danger', $('.login-form')).show();
			}
		});
		
		
		
		});
});